<?php
App::uses('Facebook', 'Lib');
class FbsController extends AppController {
	public $name = 'Fbs';
	public $uses=array('Fbuser','FbuserConfig','FriendlyUrl');
	
	function fbon($uid=null){
		if(!empty($uid)){
			//get user config
			$fbconfig=$this->FbuserConfig->find('first',array('conditions'=>$this->uses[0].'.fbid=\''.$uid.'\' and action=\'fbstatus\''));
			$fbconfig['FbuserConfig']['value']=1;
			$this->FbuserConfig->save($fbconfig);
			$this->set('data',$fbconfig['FbuserConfig']);
			$this->render('loaduserdata');
		}
		else{
			$this->render('errorloaduserdata');
		}
	}
	function fboff($uid=null){
		if(!empty($uid)){
			//get user config
			$fbconfig=$this->FbuserConfig->find('first',array('conditions'=>$this->uses[0].'.fbid=\''.$uid.'\' and action=\'fbstatus\''));
			$fbconfig['FbuserConfig']['value']=0;			
			$this->FbuserConfig->save($fbconfig);
			$this->set('data',$fbconfig['FbuserConfig']);
			$this->render('loaduserdata');
		}
		else{
			$this->render('errorloaduserdata');
		}
	}
	function removeuseractivity($id=null,$accessToken=null){
		if(!empty($id) && !empty($accessToken)){
			$config = array();
  			$config['appId'] = '204609776328294';
  			$config['secret'] = 'e88e606166871c16a52519502bec176c';
  			$facebook=new Facebook($config);
  			try {
		        $ret_obj = $facebook->api('https://graph.facebook.com/'.$id.'?access_token='.$accessToken, 'DELETE');
        		$data = $facebook->api('https://graph.facebook.com/me/news.reads?access_token='.$accessToken, 'GET');
        		t($data);
		      } catch(FacebookApiException $e) {
		        // If the user is logged out, you can have a 
		        // user ID even though the access token is invalid.
		        // In this case, we'll get an exception, so we'll
		        // just ask the user to login again here.
		        t($e);		        
		      }  
		      
			//get user config
			$activities=array();		
			$ch = curl_init('https://graph.facebook.com/'.$id.'?access_token='.$accessToken);				
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);			
			$data=(array)json_decode(curl_exec($ch));
			curl_close($ch);
			t($data);
			//get activity
			if(count($data['data'])>0){
				foreach($data['data'] as $k=>$v){
					$activities[$v->data->article->id]['url']=$v->data->article->url;
					$activities[$v->data->article->id]['title']=$v->data->article->title;
					$activities[$v->data->article->id]['time']=$v->start_time;
				}
			}
			$this->set('data',$activities);
		
		}	
		else{
			$this->render('errorloaduserdata');
		}
		
	}
	function getuseractivity($uid=null,$accessToken=null){
		if(!empty($uid) && !empty($accessToken)){
			//get user config
			$activities=array();		
			$ch = curl_init('https://graph.facebook.com/me/news.reads?access_token='.$accessToken);				
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);			
			$data=(array)json_decode(curl_exec($ch));
			curl_close($ch);
			//get activity
			if(count($data['data'])>0){
				foreach($data['data'] as $k=>$v){
					$activities[$v->data->article->id]['url']=$v->data->article->url;
					$activities[$v->data->article->id]['title']=$v->data->article->title;
					$activities[$v->data->article->id]['time']=$v->start_time;
				}
			}
			$this->set('data',$activities);
			$this->set('accessToken',$accessToken);
		
		}	
		else{
			$this->render('errorloaduserdata');
		}
		
	}
	function saveuserdata(){
		$this->layout=null;
		$this->autoRender=false;
		$data=isset($_POST['data'])?$_POST['data']:null;
		if(isset($data['id'])){
			//check is existed
			$fbconfig=$this->{$this->uses[0]}->find('first',array('conditions'=>$this->uses[0].'.fbid=\''.$data['id'].'\''));
			if(empty($fbconfig)){
				$data['fbid']=$data['id'];			
				unset($data['id']);
				$this->{$this->uses[0]}->save($data);
			}
		}
	}
	function loaduserdata($uid=null){
		$this->layout=null;
		$data=array();
		if(!empty($uid)){
			//get user config
			$fbconfig=$this->{$this->uses[1]}->find('first',array('conditions'=>$this->uses[1].'.fbid=\''.$uid.'\' and action=\'fbstatus\''));
			if(empty($fbconfig)){
				/*
				//get new user data				
				$ch = curl_init("https://graph.facebook.com/".$uid);				
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);			
				$data=(array)json_decode(curl_exec($ch));
				curl_close($ch);
				$data['fbid']=$data['id'];
				unset($data['id']);
				*/
				$data['fbid']=$uid;
				$data['action']='fbstatus';
				$data['value']='1';
				//save new user config
				$this->{$this->uses[1]}->save($data);
			}
			else{
				$data=$fbconfig[$this->uses[1]];
			}			
		}	
		$this->set('data',$data);
		
	}
	function fbjoin($uid=null){
		$this->layout=null;
		$data=array();
		$url=isset($_GET['url'])?$_GET['url']:null;
		//$uid = $fb->getUser();
		if(!empty($uid) && !empty($url)){	
			$fbconfig=$this->{$this->uses[1]}->find('first',array('conditions'=>$this->uses[1].'.fbid=\''.$uid.'\' and action=\'join\' and value=\''.$url.'\''));		
			if(!isset($fbconfig[$this->uses[1]])){
				$data['fbid']=$uid;
				$data['action']='join';
				$data['value']=$url;
				//save new user config
				$this->{$this->uses[1]}->save($data);
			}
			else{
				$data=$fbconfig[$this->uses[1]];
			}			
						
		}	
		$this->set('data',$data);
		$this->render('showjoin');
	}
	function showjoin($uid=null){
		$this->layout=null;
		$data=array();
		$url=isset($_GET['url'])?$_GET['url']:null;
		//$uid = $fb->getUser();
		if(!empty($uid) && !empty($url)){
			//get user config
			$fbconfig=$this->{$this->uses[1]}->find('first',array('conditions'=>$this->uses[1].'.fbid=\''.$uid.'\' and action=\'join\' and value=\''.$url.'\''));		
			if(isset($fbconfig[$this->uses[1]]))$data=$fbconfig[$this->uses[1]];
		}	
		$this->set('data',$data);		
	}
	


}
?>
